package com.liveneo.system.base.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class TmIfTag extends BodyTagSupport{

	/**
	 * serialVersionUID
	 */
	
	private static final long serialVersionUID = 1L;

	private String body = null;//用于存放成功添加后的内容
	private boolean subtagSucceed;
	private boolean test;
	
	public TmIfTag(){
		super();
		init();
	}
	
	
	@Override
	public int doEndTag() throws JspException {
		try {
			if(subtagSucceed){
				pageContext.getOut().write(getBody());
			}
		} catch (Exception e) {
			throw new JspException("IOError while writing the body: "
					+ e.getMessage(), e);
		}
		init();
		return super.doEndTag();
	}


	@Override
	public int doStartTag() throws JspException {
		if(test){
			this.succeed();
		}
		return EVAL_BODY_BUFFERED;
	}


	@Override
	public void release() {
		super.release();
		init();
	}

	public void setBody() {
		if (body == null) {
			body = bodyContent.getString().trim();
		}
	}

	private String getBody() {
		if (body == null)
			return bodyContent.getString().trim();
		else
			return body;
	}
	
	/**
	 * 子条件判断成功
	 */
	public void succeed() {
		subtagSucceed = true;
	}
	/**
	 * 子条件判断成功
	 */
	public boolean isSucceed() {
		return subtagSucceed;
	}
	
	private void init(){
		test = false;
		subtagSucceed = false;
		body = null;
	}
	
	public void setTest(boolean test) {
		this.test = test;
	}
}
